<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>MJ SEED</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        /* 基础样式 */
        * { 
            margin: 0; 
            padding: 0; 
            box-sizing: border-box; 
        }
        
        .container {
            display: flex;
            height: 100vh;
            background: #F5F7FA;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 240px;
            background: white;
            padding: 20px;
            border-right: 1px solid #eee;
        }

        .category-tree {
            list-style: none;
        }
        
        .category-item {
            padding: 8px 12px;
            border-radius: 4px;
            cursor: pointer;
            position: relative;
        }
        
        .category-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .arrow {
            transition: transform 0.2s;
            font-size: 0.8em;
            color: #666;
        }
        
        .expanded .arrow {
            transform: rotate(90deg);
        }
        
        .subcategory {
            display: none;
            list-style: none;
            padding-left: 20px;
            margin: 5px 0;
        }
        
        .expanded .subcategory {
            display: block;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            padding: 20px;
            position: relative;
        }

        /* 拖拽效果 */
        .dragover::after {
            content: "释放文件以上传";
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 24px;
            color: #2A7FFF;
            pointer-events: none;
            z-index: 100;
        }
        
        .dragover {
            background: rgba(42, 127, 255, 0.1) !important;
        }

        /* 缩略图网格 */
        .thumbnail-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .thumbnail-card {
            background: white;
            border-radius: 8px;
            padding: 12px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            transition: transform 0.2s;
        }
        
        .thumbnail-card:hover {
            transform: translateY(-2px);
        }
        
        .thumbnail-card img {
            width: 100%;
            height: 200px;
            object-fit: cover;
            border-radius: 4px;
            margin-bottom: 10px;
        }

        /* 顶部导航栏 */
        .navbar {
            height: 60px;
            display: flex;
            align-items: center;
            padding: 0 24px;
            background: white;
            border-bottom: 1px solid #eee;
        }
        
        .logo {
            font-weight: bold;
            color: #2A7FFF;
            margin-right: 40px;
        }
        
        .search-bar {
            flex: 1;
            max-width: 500px;
            margin: 0 20px;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        
        button {
            padding: 8px 16px;
            background: #2A7FFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 0 4px;
        }
        .profile-container{
            position: relative;
            left: 100px;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            z-index: 1000;
        }

        .modal-content {
            background-color: white;
            width: 80%;
            max-width: 400px;
            padding: 20px;
            border-radius: 8px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            max-height: 80vh;
            overflow-y: auto;
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }

        .close-btn {
            font-size: 24px;
            cursor: pointer;
            color: #666;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }

        .form-group input,
        .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .form-actions {
            text-align: right;
            margin-top: 20px;
        }

        .form-actions button {
            margin-left: 10px;
        }
    </style>
</head>
<body>  
    <!-- 顶部导航 -->
    <div class="navbar">
        <div class="logo">MJ SEED</div>
        <input type="text" class="search-bar" placeholder="搜索素材...">
        <button id="Btn">搜索</button>
        <div class="user-area">👤</div>
        <!-- 返回按钮 -->
        <div class="profile-container">
            <button class="return-btn" onclick="goBack()">返回</button>
        </div>
    </div>

    <div class="container">
        <!-- 左侧分类树 -->
        <!-- 侧边栏部分完整代码 -->
        <div class="sidebar">
            <ul class="category-tree">
                <li class="category-title">我的素材库</li>
                
                <!-- 项目A -->
                <li class="category-item">
                    <div class="category-header">
                        <span class="editable-text">项目A</span>
                        <div class="edit-controls">
                            <button class="edit-btn" onclick="startEdit(this)">✎</button>
                        </div>
                        <span class="arrow">▶</span>
                    </div>
                    <ul class="subcategory">
                        <li class="category-header">
                            <span class="editable-text">角色设计</span>
                            <div class="edit-controls">
                                <button class="edit-btn" onclick="startEdit(this)">✎</button>
                            </div>
                        </li>
                        <li class="category-header">
                            <span class="editable-text">场景原画</span>
                            <div class="edit-controls">
                                <button class="edit-btn" onclick="startEdit(this)">✎</button>
                            </div>
                        </li>
                        <li class="category-header">
                            <span class="editable-text">概念草图</span>
                            <div class="edit-controls">
                                <button class="edit-btn" onclick="startEdit(this)">✎</button>
                            </div>
                        </li>
                    </ul>
                </li>

                <!-- 项目B -->
                <li class="category-item">
                    <div class="category-header">
                        <span class="editable-text">项目B</span>
                        <div class="edit-controls">
                            <button class="edit-btn" onclick="startEdit(this)">✎</button>
                        </div>
                        <span class="arrow">▶</span>
                    </div>
                    <ul class="subcategory">
                        <li class="category-header">
                            <span class="editable-text">UI素材</span>
                            <div class="edit-controls">
                                <button class="edit-btn" onclick="startEdit(this)">✎</button>
                            </div>
                        </li>
                        <li class="category-header">
                            <span class="editable-text">图标集</span>
                            <div class="edit-controls">
                                <button class="edit-btn" onclick="startEdit(this)">✎</button>
                            </div>
                        </li>
                    </ul>
                </li>

                <!-- 未分类文件 -->
                <li class="category-item">
                    <div class="category-header">
                        <span class="editable-text">未分类文件</span>
                        <div class="edit-controls">
                            <button class="edit-btn" onclick="startEdit(this)">✎</button>
                        </div>
                    </div>
                </li>
            </ul>
        </div>

        <!-- 主工作区 -->
        <div class="main-content">
            <div class="breadcrumb">我的素材库 / 项目A / 角色设计</div>
            
            <div class="action-bar">
                <button>批量选择</button>
                <button>下载选中</button>
                <!-- 新增创建收藏夹按钮 -->
                <button onclick="openCreateCollectionModal()">创建收藏夹</button>
                <select>
                    <option>按名称排序</option>
                    <option>按日期排序</option>
                </select>
            </div>

            <!-- 缩略图区域 -->
            <div class="thumbnail-grid" >
                <!-- 默认示例内容 -->
                <div class="thumbnail-card" >
                    <a href="./detailspage.html">
                        <img src="./2420.jpg_wh860.jpg" alt="图1">
                    </a>
                    <div class="file-info">
                        <div>示例图片.jpg</div>
                        <small>2024-03-20</small>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 创建收藏夹弹窗 -->
    <div id="createCollectionModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>创建收藏夹</h3>
                <span class="close-btn" onclick="closeCreateCollectionModal()">&times;</span>
            </div>
            <form id="createCollectionForm" onsubmit="createCollection(event)">
                <div class="form-group">
                    <label for="collectionName">收藏夹名称</label>
                    <input type="text" id="collectionName" required>
                </div>
                <div class="form-actions">
                    <button type="button" class="action-btn" onclick="closeCreateCollectionModal()">取消</button>
                    <button type="submit" class="action-btn">创建</button>
                </div>
            </form>
        </div>
    </div>

    <script>

        // 打开创建收藏夹弹窗
        function openCreateCollectionModal() {
            document.getElementById('createCollectionModal').style.display = 'block';
        }

        // 关闭创建收藏夹弹窗
        function closeCreateCollectionModal() {
            document.getElementById('createCollectionModal').style.display = 'none';
        }

        // 创建收藏夹
        function createCollection(event) {
            event.preventDefault();
            const collectionName = document.getElementById('collectionName').value;
            const userId = 11; // 假设用户 ID 为 6

            if (!collectionName) {
                alert('请输入收藏夹名称');
                return;
            }

            axios({
                method: 'post',
                url: `http://127.0.0.1:5000/collect/${userId}`,
                data: {
                    collect_name: collectionName
                },
                headers: {
                    'Content-Type': 'application/json',
                }
            })
            .then(function (response) {
                if (response.data && response.data.collect_id) {
                    alert('收藏夹创建成功！');
                    closeCreateCollectionModal();
                    // 刷新页面或更新收藏夹列表
                    location.reload(); // 或者调用更新收藏夹列表的函数
                } else {
                    alert('收藏夹创建失败：' + response.data.message);
                }
            })
            .catch(function (error) {
                console.error('创建收藏夹失败：', error);
                if (error.response) {
                    alert('创建收藏夹失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('创建收藏夹失败：网络错误，请检查连接');
                }
            });
        }

        // 返回按钮
        function goBack() {
            window.history.back();
        }
        // 编辑功能实现
        let originalText = '';

        function startEdit(btn) {
            const header = btn.closest('.category-header');
            const textSpan = header.querySelector('.editable-text');
            const arrow = header.querySelector('.arrow');
            originalText = textSpan.innerText;

            // 创建输入框
            const input = document.createElement('input');
            input.className = 'edit-input';
            input.value = originalText;
            
            // 替换文本为输入框
            textSpan.innerHTML = '';
            textSpan.appendChild(input);
            input.focus();

            // 显示确认按钮
            const controls = header.querySelector('.edit-controls');
            controls.innerHTML = `
                <button class="confirm-btn" onclick="confirmEdit(this)">✓</button>
                <button class="cancel-btn" onclick="cancelEdit(this)">✕</button>
            `;

            // 隐藏箭头
            if(arrow) arrow.style.display = 'none';
        }

        function confirmEdit(btn) {
            const header = btn.closest('.category-header');
            const textSpan = header.querySelector('.editable-text');
            const input = textSpan.querySelector('input');
            const arrow = header.querySelector('.arrow');

            // 更新文本
            textSpan.innerHTML = input.value;

            // 恢复按钮
            const controls = header.querySelector('.edit-controls');
            controls.innerHTML = `<button class="edit-btn" onclick="startEdit(this)">✎</button>`;

            // 显示箭头
            if(arrow) arrow.style.display = 'inline';
        }

        function cancelEdit(btn) {
            const header = btn.closest('.category-header');
            const textSpan = header.querySelector('.editable-text');
            const arrow = header.querySelector('.arrow');

            // 恢复原始文本
            textSpan.innerHTML = originalText;

            // 恢复按钮
            const controls = header.querySelector('.edit-controls');
            controls.innerHTML = `<button class="edit-btn" onclick="startEdit(this)">✎</button>`;

            // 显示箭头
            if(arrow) arrow.style.display = 'inline';
        }
        
        // 文件夹展开/折叠功能
        document.querySelectorAll('.category-item').forEach(item => {
            const header = item.querySelector('.category-header');
            if (header) {
                header.addEventListener('click', () => {
                    item.classList.toggle('expanded');
                });
            }
        });

        
        // 创建缩略图卡片
        function createThumbnailCard({name, date, url}) {
            const card = document.createElement('div');
            card.className = 'thumbnail-card';
            card.innerHTML = `
                <img src="${url}" alt="${name}">
                <div class="file-info">
                    <div>${name}</div>
                    <small>${date}</small>
                </div>
            `;
            document.querySelector('.thumbnail-grid').appendChild(card);
        }
    </script>
</body>
</html>